package com.uobgroup.ews.uam.core.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class ProfileDataOnDemand {


   @Autowired
   private UAMRoleDataOnDemand uAMRoleDataOnDemand;
   
   @PersistenceContext
   EntityManager entityManager;
   
   public List<Profile> initProfileData(){
      List<Profile> profiles = new ArrayList<Profile>();
      Profile profile = new Profile();
      profile.setName("Profile that will be in use");
      profile.setDescription("Profile that will be in use");
      entityManager.persist(profile);
      entityManager.flush();
      profiles.add(profile);
      
      UAMUser user = new UAMUser();
      user.setProfile(profile);
      user.setIsActive(true);
      user.setProxyUser("User");
      user.setUsername("User");
      entityManager.persist(user);
      entityManager.flush();
      
      profile = new Profile();
      profile.setName("Profile that will not be in use");
      entityManager.persist(profile);
      entityManager.flush();
      profiles.add(profile);
      
      return profiles;
   }

   public void setDescription(Profile obj, int index) {
      String description = "description_" + index;
      if (description.length() > 1000) {
         description = description.substring(0, 1000);
      }
      obj.setDescription(description);
   }

   public void setName(Profile obj, int index) {
      String name = "name_" + index;
      if (name.length() > 64) {
         name = name.substring(0, 64);
      }
      obj.setName(name);
   }
}
